package site.jlopen.secret.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.lang.TypeReference;
import site.jlopen.secret.client.SecretClientHttp;
import site.jlopen.secret.client.SecretReqUrl;
import site.jlopen.secret.common.SecretClientResponse;
import site.jlopen.secret.common.SecretReqBaseForm;
import site.jlopen.secret.config.SecretClientProperties;
import site.jlopen.secret.model.graph.SecretGraphNodeParam;
import site.jlopen.secret.model.job.SecretJobCreateReq;
import site.jlopen.secret.model.job.SecretJobLogResp;
import site.jlopen.secret.service.intf.SecretJobManage;

public class SecretJobImpl implements SecretJobManage {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	private final SecretClientProperties clientProperties;
	
	public SecretJobImpl(SecretClientProperties clientProperties) {
		this.clientProperties = clientProperties;
	}

	@Override
	public SecretClientResponse<String> jobCreate(SecretJobCreateReq form) {
		logger.info("创建任务请求：{}", form);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		form.setAppId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.JOB_CREATE_URL, form);
		return res;
	}

	/**@Override
	public SecretClientResponse<String> jobApprove(String projectCode, SecretJobApproveStatus status, String reason) {
		logger.info("审批任务请求：{}", projectCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>()
				.add("projectCode", projectCode)
				.add("status", status.getStatus())
				.add("reason", reason)
				.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.JOB_APPROVE_URL, form);
		return res;
	}**/

	/**@Override
	public SecretClientResponse<String> jobStop(String projectCode) {
		logger.info("停止任务请求：{}", projectCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("projectCode", projectCode)
				.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.JOB_STOP_URL, form);
		return res;
	}**/

	/**@Override
	public SecretClientResponse<String> jobDelete(String projectCode) {
		logger.info("删除任务请求：{}", projectCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("projectCode", projectCode)
				.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.JOB_DELETE_URL, form);
		return res;
	}**/

	@Override
	public SecretClientResponse<String> jobStart(String projectCode) {
		logger.info("运行任务请求：{}", projectCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("projectCode", projectCode)
				.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.JOB_START_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> jobRestart(String projectCode) {
		logger.info("重跑任务请求：{}", projectCode);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("projectCode", projectCode)
				.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.JOB_RESTART_URL, form);
		return res;
	}

	/**@Override
	public SecretClientResponse<SecretJobStateResp> jobStatus(String projectCode) {
		logger.info("查询任务状态请求：{}", projectCode);
		SecretClientHttp<SecretJobStateResp> clientRequest = new SecretClientHttp<SecretJobStateResp>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("projectCode", projectCode)
				.appId(clientProperties.getAppId());
		SecretClientResponse<SecretJobStateResp>  res = clientRequest.execute(SecretReqUrl.JOB_STATUS_URL, form);
		return res;
	}**/

	@Override
	public SecretClientResponse<SecretJobLogResp> jobLogs(String projectCode) {
		logger.info("查询任日志务请求：{}", projectCode);
		SecretClientHttp<SecretJobLogResp> clientRequest = new SecretClientHttp<SecretJobLogResp>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("projectCode", projectCode)
				.appId(clientProperties.getAppId());
		SecretClientResponse<SecretJobLogResp>  res = clientRequest.execute(SecretReqUrl.JOB_LOGS_URL, form, new TypeReference<SecretClientResponse<SecretJobLogResp>>() {});
		return res;
	}

}
